package net.htjs.mobile;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

public class BaseActivity extends Activity{

	private static String TAG="GOBANG";
	
	PackageManager pm ;
    ResolveInfo homeInfo ;
	
    @Override
	protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	pm = this.getPackageManager();
    	homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), 0);
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (keyCode) {
			
            case KeyEvent.KEYCODE_HOME:
            	//Util.dialog(getApplicationContext(), "是否要退出应用", "退出", "后台运行", BaseActivity.this, "shutDown", null, null, null, null, false);
            	break;
            case KeyEvent.KEYCODE_BACK:
            	ActivityInfo ai = homeInfo.activityInfo;
                Intent startIntent = new Intent(Intent.ACTION_MAIN);
                startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                startIntent.setComponent(new ComponentName(ai.packageName,
                                ai.name));
                startActivitySafely(startIntent);
                return true;
            default:
            	break;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
	 void startActivitySafely(Intent intent) {
	        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	        try {
	            startActivity(intent);
	        } catch (ActivityNotFoundException e) {
	            Toast.makeText(this, R.string.app_name,
	                    Toast.LENGTH_SHORT).show();
	        } catch (SecurityException e) {
	            Toast.makeText(this, R.string.about,
	                    Toast.LENGTH_SHORT).show();
	            Log
	                    .e(
	                            TAG,
	                            "Launcher does not have the permission to launch "
	                                    + intent
	                                    + ". Make sure to create a MAIN intent-filter for the corresponding activity "
	                                    + "or use the exported attribute for this activity.",
	                            e);
	        }
	    }
}
